/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator.persist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import rentcalculator.model.Person;

/**
 *
 * @author developer
 */
public class PersonDao {
    public void add(Person p){
        Persist.getInstance().add(p);
        
        
    }
    
    
    public void remove(Person p){
        Persist.getInstance().remove(p);
    }
    
    public void update(Person p){
        Persist.getInstance().update(p);
    }
    
    
    public List<Person> findAll(){
        List<Person> ps;
        ps = (List<Person>)Persist.getInstance().findALL(Person.class);
        
        return ps;
    }
    
    public Person findById(long id){
        Object r;
        Person p;
        p = null;
        r = Persist.getInstance().findById(Person.class, id);
        if(r!= null) p = (Person)r;
        return p;
    }
    
    public List<Person> findByName(String name){
        if(name == null ) return new ArrayList<Person>();
        Map<String, String> params = new HashMap<String, String>();
        params.put("name", name);
        List<Person> lps = Persist.getInstance().find(Person.class, FIND_BY_NAME, params);
        return lps;
        
    }
    
    private final static String FIND_BY_NAME = "select * from Person "
            + "where lower(trim(name)) like concat('%',lower(:name),'%')";
    
    
    
}
